/*
 * Copyright 2017. nekocode (nekocode.cn@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nekocode.dividerdrawable;

import ohos.app.Context;

/**
 * @author nekocode (nekocode.cn@gmail.com)
 */
public class DividerLayout {
    /**
     * 水平方向
     */
    public static final int ORIENTATION_HORIZONTAL = 0;
    /**
     * 垂直方向
     */
    public static final int ORIENTATION_VERTICAL = 1;
    /**
     * 全局
     */
    public static final int MATCH_PARENT = -1;
    /**
     * 左边
     */
    public static final int ALIGN_PARENT_LEFT = 1;
    /**
     * 顶部
     */
    public static final int ALIGN_PARENT_TOP = 1 << 1;
    /**
     * 右边
     */
    public static final int ALIGN_PARENT_RIGHT = 1 << 2;
    /**
     * 底部
     */
    public static final int ALIGN_PARENT_BOTTOM = 1 << 3;
    /**
     * 不中心
     */
    public static final int CENTER_NONE = 0;
    /**
     * 水平中心
     */
    public static final int CENTER_HORIZONTAL = 1;
    /**
     * 垂直中心
     */
    public static final int CENTER_VERTICAL = 1 << 1;
    /**
     * 中心
     */
    public static final int CENTER_IN_PARENT = 0b11;

    private int orientation = ORIENTATION_HORIZONTAL;
    private int length = MATCH_PARENT;
    private int align = 0;

    private int center = CENTER_NONE;
    private int marginLeft = 0;
    private int marginTop = 0;
    private int marginRight = 0;
    private int marginBottom = 0;


    public int getOrientation() {
        return orientation;
    }

    public DividerLayout setOrientation(int orientation) {
        this.orientation = orientation;
        return this;
    }

    public int getLength() {
        return length;
    }

    public DividerLayout setLength(int length) {
        this.length = (length >= MATCH_PARENT ? length : 0);
        return this;
    }

    /**
     * 设置绘画长度
     *
     * @param context  上下文
     * @param lengthDp 长度
     * @return 布局
     */
    public DividerLayout setLengthDp(Context context, int lengthDp) {
        this.length = (lengthDp >= MATCH_PARENT ? ScreenUtils.vp2px(context, lengthDp) : 0);
        return this;
    }

    public int getAlign() {
        return align;
    }

    public DividerLayout setAlign(int align) {
        this.align = align;
        return this;
    }

    public int getCenter() {
        return center;
    }

    public DividerLayout setCenter(int center) {
        this.center = center;
        return this;
    }

    public int getMarginLeft() {
        return marginLeft;
    }

    /**
     * 设置左边边距
     *
     * @param marginLeft 边距
     * @return 布局
     */
    public DividerLayout setMarginLeft(int marginLeft) {
        this.marginLeft = marginLeft;
        return this;
    }

    /**
     * 设置左边边距
     *
     * @param context      上下文
     * @param marginLeftDp 边距
     * @return 布局
     */
    public DividerLayout setMarginLeftDp(Context context, int marginLeftDp) {
        this.marginLeft = ScreenUtils.vp2px(context, marginLeftDp);
        return this;
    }

    public int getMarginTop() {
        return marginTop;
    }

    /**
     * 设置顶部边距
     *
     * @param marginTop 边距
     * @return 布局
     */
    public DividerLayout setMarginTop(int marginTop) {
        this.marginTop = marginTop;
        return this;
    }

    /**
     * 设置底部边距
     *
     * @param context     上下文
     * @param marginTopDp 边距
     * @return 布局
     */
    public DividerLayout setMarginTopDp(Context context, int marginTopDp) {
        this.marginTop = ScreenUtils.vp2px(context, marginTopDp);
        return this;
    }

    public int getMarginRight() {
        return marginRight;
    }

    public DividerLayout setMarginRight(int marginRight) {
        this.marginRight = marginRight;
        return this;
    }

    /**
     * 设置距离右边的边距
     *
     * @param context       上下文
     * @param marginRightDp 边距
     * @return 布局
     */
    public DividerLayout setMarginRightDp(Context context, int marginRightDp) {
        this.marginRight = ScreenUtils.vp2px(context, marginRightDp);
        return this;
    }

    public int getMarginBottom() {
        return marginBottom;
    }

    public DividerLayout setMarginBottom(int marginBottom) {
        this.marginBottom = marginBottom;
        return this;
    }

    /**
     * 设置距离底部的距离
     *
     * @param context        上下文
     * @param marginBottomDp 距离
     * @return 布局
     */
    public DividerLayout setMarginBottomDp(Context context, int marginBottomDp) {
        this.marginBottom = ScreenUtils.vp2px(context, marginBottomDp);
        return this;
    }

    /**
     * 设置绘画布局的大小
     *
     * @param width       宽度
     * @param height      高度
     * @param strokeWidth 画笔的宽
     * @return 数组
     */
    public int[] layout(int width, int height, int strokeWidth) {
        final boolean alignLeft = !((align & ALIGN_PARENT_RIGHT) > 0);
        final boolean alignTop = !((align & ALIGN_PARENT_BOTTOM) > 0);
        final boolean hs = orientation == ORIENTATION_HORIZONTAL;
        final boolean ch = (center & CENTER_HORIZONTAL) > 0;
        final boolean cv = (center & CENTER_VERTICAL) > 0;

        final int[] xs;
        final int[] ys;
        xs = hs ? layoutHorizontalXAxis(ch, alignLeft, width) : layoutVerticalXAxis(ch, alignLeft, width, strokeWidth);
        ys = hs ? layoutHorizontalYAxis(cv, alignTop, height, strokeWidth) : layoutVerticalYAxis(cv, alignTop, height);

        return new int[]{xs[0], ys[0], xs[1], ys[1]};
    }


    private int[] layoutHorizontalXAxis(boolean centerHorizontal, boolean alignLeft, int width) {
        int sx;
        int ex;
        if (!centerHorizontal) {
            sx = getMarginLeft();
            ex = width - getMarginRight();
            if (length != MATCH_PARENT) {
                if (alignLeft) {
                    ex = Math.min(ex, sx + length);
                } else {
                    sx = Math.max(sx, ex - length);
                }
            }

        } else {
            int len = width - Math.max(getMarginLeft(), getMarginRight()) * 2;
            if (length != MATCH_PARENT) {
                len = Math.min(len, length);
            }

            sx = (width / 2) - (len / 2);
            ex = (width / 2) + (len / 2);
        }

        return new int[]{sx, ex};
    }

    private int[] layoutHorizontalYAxis(boolean centerVertical, boolean alignTop, int height, int strokeWidth) {
        int sy;
        int ey;
        if (!centerVertical) {
            if (alignTop) {
                sy = getMarginTop();
                ey = sy + strokeWidth;
            } else {
                ey = height - getMarginBottom();
                sy = ey - strokeWidth;
            }

        } else {
            sy = (height - strokeWidth) / 2;
            ey = sy + strokeWidth;
        }

        return new int[]{sy, ey};
    }

    private int[] layoutVerticalXAxis(boolean centerHorizontal, boolean alignLeft, int width, int strokeWidth) {
        int sx;
        int ex;
        if (!centerHorizontal) {
            if (alignLeft) {
                sx = getMarginLeft();
                ex = sx + strokeWidth;
            } else {
                ex = width - getMarginRight();
                sx = ex - strokeWidth;
            }

        } else {
            sx = (width - strokeWidth) / 2;
            ex = sx + strokeWidth;
        }

        return new int[]{sx, ex};
    }

    private int[] layoutVerticalYAxis(boolean centerVertical, boolean alignTop, int height) {
        int sy;
        int ey;
        if (!centerVertical) {
            sy = getMarginTop();
            ey = height - getMarginBottom();
            if (length != MATCH_PARENT) {
                if (alignTop) {
                    ey = Math.min(ey, sy + length);
                } else {
                    sy = Math.max(sy, ey - length);
                }
            }

        } else {
            int len = height - Math.max(getMarginTop(), getMarginBottom()) * 2;
            if (length != MATCH_PARENT) {
                len = Math.min(len, length);
            }
            sy = (height / 2) - (len / 2);
            ey = (height / 2) + (len / 2);
        }

        return new int[]{sy, ey};
    }


}
